<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
      @reset="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="业务名称" name="businessName">
            <t-input v-model="queryParams.businessName" clearable placeholder="请输入项目名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="业务类型" name="approveType">
            <t-select style="width: 240px" clearable v-model="queryParams.approveType"
              :options="dict.type.wcx_approve_type">
            </t-select>
          </t-form-item>
          <t-form-item label="填报单位" name="deptName">
            <t-input v-model="queryParams.deptName" clearable placeholder="请输入填报单位" style="width: 240px" />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
      <t-row style="margin-bottom: 10px">
        <t-col :span="12" class="">
          <t-button @click="exportExcelOther" variant="outline" theme="primary">
            <t-icon name="download" slot="icon" />
            导出待我审批项目
          </t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table rowKey="id" :data="data"
      :columns="!hideInfo ? columns : columns.filter(val => { return val.title !== '填报单位' })" hover :sort="sort"
      :pagination="pagination" :loading="dataLoading" :hideSortTips="true" @sort-change="sortChange">
      <template #[`innovations.name`]="{ row }">
        <t-link
          @click="dialog.objId = row.innovations.id; dialog.header = row.innovations.name; dialog.open_innovation_detail = true"
          hover="color" theme="primary">{{ row.innovations.name }}</t-link>
      </template>
      <template #[`innovations.innovationStatus`]="{ row }">
        <t-tag @click="dialog.header = '审批记录'; dialog.objId = row.innovations.id; dialog.open_approve_list = true"
          class="cursor-pointer" :theme="handleTheme(row.innovations.innovationStatus, dict.type.innovation_status)"
          variant="light">
          {{ row.innovations.innovationStatus | filterByDict(dict.type.innovation_status) }}
        </t-tag>
      </template>
      <template #zan="{ row }">
        <t-tag v-if="row.zan === '1'" theme="warning">暂存</t-tag>
        <t-tag v-else theme="warning">待审批</t-tag>
      </template>
      <template #approveType="{ row }">
        {{ row.approveType | filterByDict(dict.type.wcx_approve_type) }}
      </template>
      <template #op="{ row }">
        <t-button @click="dialog.row = row; dialog.open = true" size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          审批
        </t-button>
      </template>
    </t-table>
    <div style="color: red">注意:本单位及归口管理单位请在规定的时间内({{ isGkgldw ? planData.recommendStartDate : planData.spStartDate }} 至
      {{
        isGkgldw ? planData.recommendEndDate : planData.spEndDate }} 23:59:59)完成项目审批，否则流程不能继续流转，影响项目申报!</div>

    <approve-info :visible.sync="dialog.open" @reload="getList()" :row="dialog.row" v-if="dialog.open">
    </approve-info>
    <innovation-detail-dialog :header="dialog.header" :objId="dialog.objId"
      :visible.sync="dialog.open_innovation_detail" v-if="dialog.open_innovation_detail"></innovation-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
      v-if="dialog.open_approve_list"></approve-list>

  </div>
</template>

<script>
import ApproveInfo from "./components/approve-info.vue"
import InnovationDetailDialog from "../innovation/components/innovation-detail-dialog.vue";
import ApproveList from "./components/approve-list.vue";

export default {
  name: "index",
  dicts: ["wcx_approve_type", "innovation_status"],
  components: {
    ApproveInfo,
    InnovationDetailDialog,
    ApproveList
  },
  data () {
    return {
      hideInfo: this.$store.state.user.roles.includes("wcxXssc"),
      dialog: {
        open: false,
        header: "",
        row: {},
        data: {},
        open_innovation_detail: false,
        objId: 0,
        open_approve_list: false
      },
      planData: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approveType: '',
        businessName: '',
        deptName: ''
      },
      data: [],
      sort: [],
      columns: [
        // {
        //   colKey: "innovations.id",
        //   title: "成果编号",
        //   align: "center",
        //   ellipsis: true
        // },
        {
          colKey: "innovations.name",
          title: "成果名称",
          align: "center",
          // ellipsis: true,
          width: 220,
        },
        {
          colKey: "innovations.innovationStatus",
          title: "成果状态",
          align: "center",
          width: 120
        },
        // {
        //   colKey: "businessId",
        //   title: "业务id",
        //   align: "center",
        //   ellipsis: true
        // },
        // {
        //   colKey: "businessName",
        //   title: "业务名称",
        //   align: "center",
        // },
        {
          colKey: "approveType",
          title: "审批业务类型",
          align: "center",
        },
        {
          colKey: "innovations.declareDeptName",
          title: "填报单位",
          align: "center",
          sorter: true
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center",
          width: 200,
          sorter: true
        },
        {
          colKey: "zan",
          title: "审批结果",
          align: "center",
          width: 100
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 200
        },
      ],
      isGkgldw: false,
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  async created () {
    this.getList()
    this.getThisYearPlan()
    this.getIsGkgldw()
  },
  methods: {
    exportExcelOther () {
      this.$download(`wcx/excel/approveList`, this.queryParams, `待我审批列表_${new Date().getTime()}.xls`)
    },
    getIsGkgldw () {
      this.$api.system.dept.isAttributeDept().then(response => {
        this.isGkgldw = response.data
      })
    },
    getThisYearPlan () {
      return this.$api.wcx.apply.getThisYearPlan().then(response => {
        this.planData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    sortChange (sort) {
      if (sort instanceof Array) {
        sort.forEach(item => {
          item.sortBy = item.sortBy.substring(item.sortBy.indexOf('.') + 1);
        })
      } else {
        sort.sortBy = sort.sortBy.substring(sort.sortBy.indexOf('.') + 1);
      }

      let orderByColumn = []
      let isAsc = []
      if (sort instanceof Array) {
        orderByColumn = sort.map(val => val.sortBy)
        isAsc = sort.map(val => val.descending ? `desc` : `asc`)
      } else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending ? `desc` : `asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    getList () {
      this.dataLoading = true;
      this.$api.wcx.approve.myApproveList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    // 审批
    opApproveFun (row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否审核`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjxm.approve.opApprove({
            id: row.id,
            businessId: row.businessId,
            approveType: row.approveType,
            opinion: "通过",
            approveStatus: "3",
          }).then(
            (res) => {
              this.getList();
              this.$message.success("审核成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
        onClose: ({ e, trigger }) => {
          this.$api.kjxm.approve.opApprove({
            id: row.id,
            businessId: row.businessId,
            approveType: row.approveType,
            opinion: "退回",
            approveStatus: "4",
          }).then(
            (res) => {
              this.getList();
              this.$message.success("审核成功");
              confirmDia.hide();
            }
          ).catch((e) => {
            this.$message.error(e.toString());
          });

        },
      })
      // this.dialog.header = '审批';
      // this.dialog.data = data.row;
      // this.dialog.open_add_edit = true;
    },
  },
}
</script>

<style scoped></style>
